﻿using AiMoBridge.Interface;
using AiMoWindows.MainActions.LibraryActions;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace AiMoWindows.Windows.CommandLibrary.ViewModels
{
    class CommandLibraryViewModel : ViewModelBase
    {
        readonly CommandLibraryWindow mainWindow;
        readonly LibraryActionsController actions;
        public CommandLibraryViewModel(CommandLibraryWindow window, IExecutor? executor)
        {
            actions = new LibraryActionsController(window, executor, this);
            mainWindow = window;
            CommanLibraryUI = new CommandLibraryUIViewModel(window);
            ImportScriptsCommmand = new RelayCommand(actions.ImportScriptsButton_Clicked);
            ExportRhpCommmand = new RelayCommand(actions.ExportRhpButton_Clicked);
            RunCommmand = new RelayCommand(actions.RunButton_Click);
            DownloadScript = new RelayCommand(actions.DownloadScriptButton_Click);
        }
        public ObservableCollection<CommandItemViewModel> CommandItems { get; set; } = new ObservableCollection<CommandItemViewModel>();
        public ICommand ImportScriptsCommmand { get; }
        public ICommand ExportRhpCommmand { get; }
        public ICommand RunCommmand { get; }
        public ICommand DownloadScript { get; }
        public CommandLibraryUIViewModel CommanLibraryUI { get; }
        CommandItemViewModel? _SelectedCommandItem;
        public CommandItemViewModel? SelectedCommandItem
        {
            get => _SelectedCommandItem;
            set
            {
                _SelectedCommandItem = value;
                RaisePropertyChanged(nameof(SelectedCommandItem));
            }
        }
        public async void Onload()
        {
            await mainWindow.Dispatcher.InvokeAsync(actions.OnLoad);
        }
        public async void RemoveCommandItem(CommandItemViewModel commandItem)
        {
            await mainWindow.Dispatcher.InvokeAsync(() =>
            {
                CommandItems.Remove(commandItem);
                commandItem.RemoveCurrent -= RemoveCommandItem;
            });
        }
        public void ClearPrint()
        {
            mainWindow.ClearPrint();
        }
        public void ShowMessage(string message)
        {
            MessageBox.Show(mainWindow, message);
        }
    }
}